package controller.filemanager;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import model.filemanager.FileManager;
import model.filemanager.MediaFile;
import model.filemanager.MediaList;

@SuppressWarnings("serial")
public class MediaListServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse res) {
		String command =  req.getParameter("command");
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		FileManager fm = new FileManager(user.getEmail());
		
		
		if(command == null)
			return;
		
		if(command.equals("addlist")){		
			String name = req.getParameter("name");
			String type = req.getParameter("type");
			MediaList.AddList(name, type, user.getEmail());
			try {
				res.sendRedirect(req.getHeader("Referer"));
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		}
		
		if(command.equals("removelist")){
			String key = req.getParameter("key");
			String name = req.getParameter("name");
			String type = req.getParameter("type");
			if(key != null){
				long listKey;
				try
				{
					listKey = Integer.parseInt(key);
				}
				catch(NumberFormatException e)
				{
					listKey = -1;
				}
				MediaList.RemoveList(listKey);
				return;
			}else{
				MediaList.RemoveList(name, type, user.getEmail());
				return;
			}
		}
		
		if(command.equals("getlists")){
			StringBuffer retString = new StringBuffer();
			String name = req.getParameter("name");
			String type = req.getParameter("type");
			Iterator<Entity> lists = MediaList.getLists(type, user.getEmail()).iterator();
			while(lists.hasNext()){
				Entity list = (Entity) lists.next();
				retString.append(list.getProperty("name"));
				retString.append(";");
			}
			return;
		}
		
		if(command.equals("addtolist")){
			String listP = req.getParameter("listkey");
			String key = req.getParameter("mediakey");
			long listKey = -1;
			if (listP != null)
			{
				try
				{
					listKey = Integer.parseInt(listP);
				}
				catch(NumberFormatException name)
				{
					listKey = -1;
				}
			}
			if(listKey!=-1 && key!=null){
					Entity listEnt = MediaList.getListEntity(listKey);
					MediaFile media = null;
					try {
						media = fm.GetFile(key);
					} catch (EntityNotFoundException e) {
					}
					if(media!=null){
						MediaList.addMedia(listEnt.getKey().getId(), key);
					}
				
			}
			return;
		}
		
		if(command.equals("removefromlist")){
			
			String listP = req.getParameter("listkey");
			String key = req.getParameter("mediakey");
			
			long listKey = -1;
			if (listP != null)
			{
				try
				{
					listKey = Integer.parseInt(listP);
				}
				catch(NumberFormatException name)
				{
					listKey = -1;
				}
			}
			if(listKey!=-1 && key!=null){
					Entity listEnt = MediaList.getListEntity(listKey);
					MediaFile media = null;
					try {
						media = fm.GetFile(key);
					} catch (EntityNotFoundException e) {
					}
					if(media!=null){
						MediaList.removeMedia(listEnt.getKey().getId(), key);
					}
				
			}
			return;
			
		}
		
		
	}
}
